package com.gimi.cloud.bbp.manage.controller.sys;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.sys.SysRoleDTO;
import com.gimi.cloud.bbp.manage.service.sys.SysRoleService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理相关Controller
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-26
 **/
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
    private SysRoleService sysRoleService;

    @Autowired
    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    /**
     * 获取角色列表
     * @param request
     * @param response
     * @param roleDTO 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response, @RequestBody SysRoleDTO roleDTO) throws  Exception{
        Response resp = new Response();
        List<SysRoleDTO> roles = sysRoleService.queryList(roleDTO,true);
        Map<String,Object> resData = new HashMap<>();
        PageInfo<SysRoleDTO> pageInfo = new PageInfo<>(roles);
        resData.put(ServiceNameConstants.PAGEDATA_KEY,pageInfo);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取角色信息
     * @param request
     * @param response
     * @param roleDTO 查询参数
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody SysRoleDTO roleDTO) throws Exception{
        Response resp = new Response();
        SysRoleDTO role = sysRoleService.load(roleDTO, roleDTO.getRoId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("role",role);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 保存角色信息
     * @param request
     * @param response
     * @param roleDTO 待保存的角色信息
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@RequestBody SysRoleDTO roleDTO) throws Exception{
        Response resp = new Response();
        sysRoleService.saveOrUpdate(roleDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
        return resp;
    }

    /**
     * 删除角色信息
     * @param request
     * @param response
     * @param roleDTO 待删除的角色信息
     * @throws Exception
     */
    @RequestMapping("/delete")
    public Response delete(HttpServletRequest request, HttpServletResponse response, @RequestBody SysRoleDTO roleDTO) throws  Exception{
        Response resp = new Response();
        sysRoleService.delete(roleDTO,roleDTO.getRoId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }
}